/**
 * Progetto PRAISED.
 * 27/gen/2011
 */
package it.uniroma3.dia.praised.core.model;

import java.io.IOException;

import it.uniroma3.dia.praised.core.util.Utils;

/**
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 *
 */
public class Rank implements Comparable<Rank> {
	
	private double editRank;
	private double jaccardRank;
	private double weightedRank;
	private double totalRank;
	private double weight_jaccard;
	private double weight_edit;
	private double weight_weighted;

	/**
	 * 
	 */
	public Rank() {
		try {
			this.weight_edit = Double.valueOf(Utils.loadProperty("Config.properties", "weight_edit"));
			this.weight_jaccard = Double.valueOf(Utils.loadProperty("Config.properties", "weight_jaccard"));
			this.weight_weighted = Double.valueOf(Utils.loadProperty("Config.properties", "weight_weighted"));

		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param editRank the editRank to set
	 */
	public void setEditRank(double editRank) {
		this.editRank = editRank;
	}

	/**
	 * @return the editRank
	 */
	public double getEditRank() {
		return this.editRank;
	}

	/**
	 * @param jaccardRank the jaccardRank to set
	 */
	public void setJaccardRank(double jaccardRank) {
		this.jaccardRank = jaccardRank;
	}

	/**
	 * @return the jaccardRank
	 */
	public double getJaccardRank() {
		return this.jaccardRank;
	}

	/**
	 * @param weightedRank the weightedRank to set
	 */
	public void setWeightedRank(double weightedRank) {
		this.weightedRank = weightedRank;
	}

	/**
	 * @return the weightedRank
	 */
	public double getWeightedRank() {
		return this.weightedRank;
	}

	/**
	 * @return the totalRank
	 */
	public double getTotalRank() {
		return this.totalRank;
	}

	/**
	 * @param totalRank the totalRank to set
	 */
	public void setTotalRank(double totalRank) {
		this.totalRank = totalRank;
	}
	
	/**
	 * @return the weight_jaccard
	 */
	public double getWeight_jaccard() {
		return this.weight_jaccard;
	}

	/**
	 * @param weightJaccard the weight_jaccard to set
	 */
	public void setWeight_jaccard(double weightJaccard) {
		this.weight_jaccard = weightJaccard;
	}

	/**
	 * @return the weight_edit
	 */
	public double getWeight_edit() {
		return this.weight_edit;
	}

	/**
	 * @param weightEdit the weight_edit to set
	 */
	public void setWeight_edit(double weightEdit) {
		this.weight_edit = weightEdit;
	}

	/**
	 * @return the weight_weighted
	 */
	public double getWeight_weighted() {
		return this.weight_weighted;
	}

	/**
	 * @param weightWeighted the weight_weighted to set
	 */
	public void setWeight_weighted(double weightWeighted) {
		this.weight_weighted = weightWeighted;
	}
	
	/**
	 * 
	 */
	public void generateTotalRankThroughtWeightedFunction() {
		double totRank = ((this.getEditRank() * this.getWeight_edit()) +
						  (this.getJaccardRank() * this.getWeight_jaccard()) +
						  (this.getWeightedRank() * this.getWeight_weighted()));
		
		this.setTotalRank(transformRankToPercentage(totRank));
	}
	
	private double transformRankToPercentage(double rank) {
		return (1-Double.parseDouble("0." + Math.round(rank))) * 100;
	}

	@Override
	public int compareTo(Rank r) {
		int result = 0;
		
		if(this.totalRank > r.getTotalRank())
			result = -1;
		if(this.totalRank < r.getTotalRank())
			result = 1;
		
		return result;
	}
	
	/**
	 * 
	 */
	public String toString() {
		return Double.toString(this.totalRank);
	}

}
